package com.lucien.framework.websocket;

import com.lucien.framework.websocket.interceptor.GetHeaderParamInterceptor;
import com.lucien.framework.websocket.interceptor.MylHandshakeHandler;
import com.lucien.framework.websocket.interceptor.WebSocketChannelInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration;

/**
 * @Author Lucien
 * @Date 2021/4/20 10:14
 * @Version 1.0
 * @Description: websocket配置文件
 * <p>
 * https://blog.csdn.net/weixin_40693633/article/details/91512632
 * https://blog.csdn.net/zhenghuasheng/article/details/80621121
 */
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    private static final Logger log = LoggerFactory.getLogger(WebSocketConfig.class);

    @Autowired
    private GetHeaderParamInterceptor interceptor;

    @Autowired
    private WebSocketChannelInterceptor channelInterceptor;

    @Autowired
    private MylHandshakeHandler handshakeHandler;

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        //暴露节点
        registry.addEndpoint("/ws/chatroom") // 端点名称
                .setAllowedOrigins("*")// 跨域支持
                .withSockJS()//使用 sockjs
                .setInterceptors(handshakeHandler);// 握手拦截器
    }


    /**
     * 使用RabbitMQ做为消息代理，替换默认的Simple Broker
     * 配置消息代理
     *
     * @param registry
     */
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        //sendToUser使用,前端订阅需要加上/user
        registry.setUserDestinationPrefix("/user");
        //客户端推送消息到服务器使用 ，推送的接口加上/app。客户端程序访问服务器，需带有/app 前缀
        registry.setApplicationDestinationPrefixes("/chatroom");

        //暴露的节点上，消息代理即将会处理 前缀为 /topic 和 /queue 的请求消息
        registry.enableStompBrokerRelay("/exchange", "/topic", "/queue", "/amq/queue")
                .setRelayHost("127.0.0.1")
                .setClientLogin("guest")
                .setClientPasscode("guest")
                .setSystemLogin("guest")
                .setSystemPasscode("guest")
                .setSystemHeartbeatSendInterval(5000)
                .setSystemHeartbeatReceiveInterval(4000);
        log.info("===websocket注入===");
    }

    /**
     * 采用自定义拦截器，获取connect时候传递的参数
     *
     * @param registration
     */
    @Override
    public void configureClientInboundChannel(ChannelRegistration registration) {
        registration.interceptors(interceptor);
        registration.interceptors(channelInterceptor);
    }


    /**
     * 配置发送与接收的消息参数，可以指定消息字节大小，缓存大小，发送超时时间
     *
     * @param registry
     */
    @Override
    public void configureWebSocketTransport(WebSocketTransportRegistration registry) {
        /*
         * 1. setMessageSizeLimit 设置消息缓存的字节数大小 字节
         * 2. setSendBufferSizeLimit 设置websocket会话时，缓存的大小 字节
         * 3. setSendTimeLimit 设置消息发送会话超时时间，毫秒
         */
        registry.setMessageSizeLimit(10240)
                .setSendBufferSizeLimit(10240)
                .setSendTimeLimit(10000);
    }
}
